package com.xidian.zhiancloud.service.exam.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xidian.zhiancloud.dao.exam.TTrainContentMapper;
import com.xidian.zhiancloud.dao.exam.TTrainPlanMapper;
import com.xidian.zhiancloud.dao.exam.TrainPlanUserMapper;
import com.xidian.zhiancloud.model.exam.PlanUserMark;
import com.xidian.zhiancloud.model.exam.TrainPlan;
import com.xidian.zhiancloud.qo.BaseReqQuery;
import com.xidian.zhiancloud.service.exam.ITrainPlanService;
import com.xidian.zhiancloud.util.api.DateUtils;

/**
 * 培训计划Service业务层处理
 * 
 * @author zhouweitao
 * @date 2021-03-16
 */
@Service
public class TrainPlanServiceImpl implements ITrainPlanService 
{
    @Autowired
    private TTrainPlanMapper trainPlanMapper;
    
    @Autowired
    private TrainPlanUserMapper trainPlanUserMapper;
    
    @Autowired
    private TTrainContentMapper trainContentMapper;

    /**
     * 查询培训计划
     * 
     * @param id 培训计划ID
     * @return 培训计划
     */
    @Override
    public TrainPlan selectTrainPlanById(Long id)
    {
    	
    	
        return trainPlanMapper.selectTrainPlanById(id);
    }

    /**
     * 查询培训计划列表
     * 
     * @param trainPlan 培训计划
     * @return 培训计划
     */
    @Override
    public Page<TrainPlan> selectTrainPlanList(TrainPlan trainPlan)
    {
    	PageHelper.startPage(trainPlan.getPageNum()-1, trainPlan.getPageSize());
        return trainPlanMapper.selectTrainPlanList(trainPlan);
    }

    /**
     * 新增培训计划
     * 
     * @param trainPlan 培训计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTrainPlan(TrainPlan trainPlan)
    {
    	trainPlanMapper.insertTrainPlan(trainPlan);
    	if(trainPlan !=null && trainPlan.getUsers().size()>0) {
    		for (int i = 0; i < trainPlan.getUsers().size(); i++) {
    			trainPlan.getUsers().get(i).setPlanId(trainPlan.getId());
			}
    		trainPlanUserMapper.insertTrainPlanUserBatch(trainPlan.getUsers());
    	}
    	
    	if(trainPlan !=null && trainPlan.getContents().size()>0) {
    		for (int i = 0; i < trainPlan.getContents().size(); i++) {
    			trainPlan.getContents().get(i).setTrainPlanId(trainPlan.getId());
			}
    		trainContentMapper.insertTrainContentBatch(trainPlan.getContents());
    	}
    	
        return 1;
    }

    /**
     * 修改培训计划
     * 
     * @param trainPlan 培训计划
     * @return 结果
     */
    @Override
    @Transactional
    public int updateTrainPlan(TrainPlan trainPlan)
    {
    	trainPlanUserMapper.deleteTrainPlanUserByPlanId(trainPlan.getId());
    	trainContentMapper.deleteTrainContentByPlanId(trainPlan.getId());
    	if(trainPlan !=null && trainPlan.getUsers().size()>0) {
    		for (int i = 0; i < trainPlan.getUsers().size(); i++) {
    			trainPlan.getUsers().get(i).setPlanId(trainPlan.getId());
			}
    		trainPlanUserMapper.insertTrainPlanUserBatch(trainPlan.getUsers());
    	}
    	
    	if(trainPlan !=null && trainPlan.getContents().size()>0) {
    		for (int i = 0; i < trainPlan.getContents().size(); i++) {
    			trainPlan.getContents().get(i).setTrainPlanId(trainPlan.getId());
			}
    		trainContentMapper.insertTrainContentBatch(trainPlan.getContents());
    	}
        return trainPlanMapper.updateTrainPlan(trainPlan);
    }

    /**
     * 批量删除培训计划
     * 
     * @param ids 需要删除的培训计划ID
     * @return 结果
     */
    @Override
    public int deleteTrainPlanByIds(Long[] ids)
    {
        return trainPlanMapper.deleteTrainPlanByIds(ids);
    }

    /**
     * 删除培训计划信息
     * 
     * @param id 培训计划ID
     * @return 结果
     */
    @Override
    public int deleteTrainPlanById(Long id)
    {
        return trainPlanMapper.deleteTrainPlanById(id);
    }

	@Override
	public Page<PlanUserMark> selectTrainPlanUserMarkById(Long id) {
		// TODO Auto-generated method stub
		return trainPlanMapper.selectTrainPlanUserMarkById(id);
	}

	@Override
	public Page<TrainPlan> getPlanByUser(BaseReqQuery base, Long id) {
		// TODO Auto-generated method stub
		PageHelper.startPage(base.getPageNum()-1, base.getPageSize());
        return trainPlanMapper.getPlanByUser(base,id);
	}
}
